﻿using AutoMapper;
using MediatR;
using WMS.API.Application.Command.StocktakingManagement.PendingOrder;
using WMS.Domain.Entities.StocktakingManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.StocktakingManagement
{
    /// <summary>
    /// 新增待盘点订单命令处理-xzc
    /// </summary>
    public class AddPendingOrdersCommandHandlers : IRequestHandler<AddPendingOrdersCommand, APIResult<int>>
    {
        private readonly IBaseRepository<PendingOrders> _pendingordersRepository;
        private readonly IMapper mapper;

        public AddPendingOrdersCommandHandlers(IBaseRepository<PendingOrders> pendingorders, IMapper mapper)
        {
            _pendingordersRepository = pendingorders;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(AddPendingOrdersCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>("新增待盘点订单成功");

            result.Code = ResultCode.Success;
            
            PendingOrders pendingOrders = mapper.Map<PendingOrders>(request);

            await _pendingordersRepository.AddAsync(pendingOrders);
            
            return await Task.FromResult(result);
        }
    }
}
